package com.ning.farm_income_back.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ning.farm_income_back.domain.Costs;
import com.ning.farm_income_back.domain.Land;
import com.ning.farm_income_back.domain.Revenue;
import com.ning.farm_income_back.domain.Users;
import com.ning.farm_income_back.dto.RevenueDto;
import com.ning.farm_income_back.mapper.LandMapper;
import com.ning.farm_income_back.mapper.RevenueMapper;
import com.ning.farm_income_back.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
public class RevenueService {

    @Autowired
    private RevenueMapper revenueMapper;

    @Autowired
    private LandMapper landMapper;

    @Autowired
    private UsersMapper usersMapper;

    //获取页面服务
    public PageInfo getPage(RevenueDto revenueDto, Integer pageNum, Integer pageSize) {

        //排序
        PageHelper.startPage(pageNum,pageSize,"revenue_id desc ");

        List<RevenueDto> list = revenueMapper.selectBy(revenueDto);

        return new PageInfo<>(list);
    }

    //新增收益服务
    public void saveRevenueInfo(RevenueDto revenueDto, String username) {
        Land l = landMapper.selectByUsed(revenueDto.getUsed());
        Users u = usersMapper.selectByUsername(username);
        Date date = new Date();
        Revenue r = revenueDto;
        r.setLandId(l.getLandId());
        r.setRecorderId(u.getUserId());
        r.setTransactionDate(date);
        revenueMapper.insert(r);
    }

    public void updateRevenueInfo(RevenueDto revenueDto, String username) {
        Land l = landMapper.selectByUsed(revenueDto.getUsed());
        Users u = usersMapper.selectByUsername(username);
        Date date = new Date();
        Revenue r = revenueDto;
        r.setLandId(l.getLandId());
        r.setRecorderId(u.getUserId());
        r.setTransactionDate(date);
        revenueMapper.updateByPrimaryKey(r);
    }

    public void deleteRevenueInfo(Integer id) {
        revenueMapper.deleteByPrimaryKey(id.longValue());
    }

    public List<String> getRevenueEveryYear(String startY, String endY) {
        return revenueMapper.selectEveryYear(startY,endY);
    }

    public void updateRevenueStatus(Revenue revenue) {
        revenue.setReviewDate(new Date());
        revenueMapper.updateByPrimaryKey(revenue);
    }

    public void rejectRevenue(Revenue revenue) {
        revenue.setReviewDate(new Date());
        revenueMapper.updateByPrimaryKey(revenue);
    }
}
